﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
using DBScript;
using eBlog.Entity;

namespace eBlog.Service
{
    /// <summary>
    /// 配置管理
    /// </summary>
    public class SettingService
    {

        /// <summary>
        /// 静态变量
        /// </summary>
        private static SettingInfo _setting;

        /// <summary>
        /// lock
        /// </summary>
        private static object lockHelper = new object();

        static SettingService()
        {
            LoadSetting();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public static void LoadSetting()
        {
            if (_setting == null)
            {
                lock (lockHelper)
                {
                    if (_setting == null)
                    {
                        _setting = GetSetting();
                    }
                }
            }
        }

        /// <summary>
        /// 初始化配置
        /// </summary>
        public static bool InitializeSetting()
        {
            IDBBase dal = DBFactory.GetDbProvider();

            var settingInfoModel = new SettingInfo();

            Sites sites = dal.GetModel<Sites>(1);

            sites.Setting = Serialize(settingInfoModel);

            return dal.Update(sites);

        }


        /// <summary>
        /// 获取
        /// </summary>
        /// <returns></returns>
        public static SettingInfo GetSetting()
        {
            IDBBase dal = DBFactory.GetDbProvider();
            var setting = dal.GetModel<Sites>(1).Setting;

            var str = Convert.ToString(setting);

            object obj = DeSerialize(typeof(SettingInfo), str);
            if (obj == null)
            {
                return new SettingInfo();
            }

            return (SettingInfo)obj;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public static bool UpdateSetting(int siteId,SettingInfo settingInfo)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            Sites sites = dal.GetModel<Sites>(siteId);
            sites.Setting = Serialize(settingInfo);
            dal.Update(sites);
            return true;
        }


        #region Serialize&DeSerialize

        /// <summary>
        /// xml序列化成字符串
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>xml字符串</returns>
        public static string Serialize(object obj)
        {
            string returnStr;

            XmlSerializer serializer = new XmlSerializer(obj.GetType());


            MemoryStream ms = new MemoryStream();
            XmlTextWriter xtw = null;
            StreamReader sr = null;
            try
            {
                xtw = new XmlTextWriter(ms, Encoding.UTF8) {Formatting = Formatting.Indented};
                serializer.Serialize(xtw, obj);
                ms.Seek(0, SeekOrigin.Begin);
                sr = new StreamReader(ms);
                returnStr = sr.ReadToEnd();
            }
            finally
            {
                if (xtw != null)
                    xtw.Close();
                if (sr != null)
                    sr.Close();
                ms.Close();
            }
            return returnStr;

        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="type"></param>
        /// <param name="s"></param>
        /// <returns></returns>
        public static object DeSerialize(Type type, string s)
        {
            byte[] b = Encoding.UTF8.GetBytes(s);
            try
            {
                XmlSerializer serializer = new XmlSerializer(type);

                return serializer.Deserialize(new MemoryStream(b));
            }
            catch
            {
                //  throw ex;
                return null;
            }
        }

        #endregion
    }
}
